package random.fox.p2p.ui;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.util.Calendar;

import javax.inject.Inject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import flow.Flow;
import random.fox.p2p.App;
import random.fox.p2p.IndexActivity;
import random.fox.p2p.Paths;
import random.fox.p2p.R;
import random.fox.p2p.event.FinishActivityEvent;
import random.fox.p2p.event.GetPicEvent;
import random.fox.p2p.event.PickPicEvent;
import random.fox.p2p.event.ProgressEvent;
import random.fox.p2p.model.SetInfoPresenterModule;
import random.fox.p2p.pathview.HandlesBack;
import random.fox.p2p.presenter.SetInfoPresenter;
import random.fox.p2p.ui.customview.imageLoader.CircleBitmapDisplayer;
import random.fox.p2p.util.Util;
import random.fox.p2p.view.ISetInfoView;

/**
 * 用户信息设置界面
 * Created by wq on 15/3/14.
 */
public class SetInfoView extends LinearLayout implements HandlesBack,ISetInfoView {

    @InjectView(R.id.upload_head)ImageView headView;
    @InjectView(R.id.edt_set_nickname)EditText edtNickName;
    @InjectView(R.id.tv_sex_female)TextView sexFemale;
    @InjectView(R.id.tv_sex_male)TextView sexMale;
    @InjectView(R.id.edt_set_school)EditText edtSchool;
    @InjectView(R.id.edt_set_city)EditText edtCity;
    @InjectView(R.id.edt_set_birthday)TextView edtBirthday;

    @Inject SetInfoPresenter presenter;

    private int sex = -1;
    private File headImage;

    public SetInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ButterKnife.inject(this);
        ((App)getContext().getApplicationContext())
                .createScopedGraph(new SetInfoPresenterModule(this)).inject(this);
        EventBus.getDefault().register(this);

    }

    @Override
    public boolean onBackPressed() {
        AlertDialog dialog =
                new AlertDialog.Builder(getContext(),AlertDialog.THEME_DEVICE_DEFAULT_DARK).create();
        dialog.setTitle("是否跳过设置?");
        dialog.setMessage("可以在设置中进行设置.");
        dialog.setButton(dialog.BUTTON_POSITIVE,"确定"
                ,new DialogInterface.OnClickListener(){

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                dialog.dismiss();
                EventBus.getDefault().post(new FinishActivityEvent());
                Intent intent = new Intent(getContext(), IndexActivity.class);
                getContext().startActivity(intent);
            }
        });
        dialog.setButton(dialog.BUTTON_NEGATIVE, "取消"
                , new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
        return true;
    }

    /**
     * 上传头像点击事件
     */
    @OnClick(R.id.upload_head) void onUpladClicked(){
        AlertDialog dialog =
                new AlertDialog.Builder(getContext(),AlertDialog.THEME_DEVICE_DEFAULT_DARK).create();
        dialog.setTitle("选择图片");
        dialog.setButton(dialog.BUTTON_POSITIVE,"拍照",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EventBus.getDefault().post(new PickPicEvent(Util.CAMERA));
            }
        });
        dialog.setButton(dialog.BUTTON_NEGATIVE,"相册选取",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                EventBus.getDefault().post(new PickPicEvent(Util.GALLERY));
            }
        });
        dialog.show();
    }

    public void onEvent(GetPicEvent event){
        headImage = event.getPicFile();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .displayer(new CircleBitmapDisplayer())
                .build();
        ImageLoader.getInstance().displayImage(Uri.fromFile(headImage).toString(),headView,options);
    }

    @OnClick(R.id.edt_set_birthday) void onSetBirthdayClicked(){
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        DatePickerDialog dialog = new DatePickerDialog(getContext(),new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                String month = monthOfYear+"";
                if (monthOfYear<10){
                    month="0"+month;
                }
                String day = dayOfMonth+"";
                if (dayOfMonth<10){
                    day = "0"+day;
                }
                String birthday =  year+"-"+month+"-"+day;
                edtBirthday.setText(birthday);
            }
        },year,month,day);

        dialog.show();
    }

    @OnClick(R.id.tv_sex_female) void onFemaleSelected(){
        sex =1;
        sexFemale.setBackgroundResource(R.drawable.sex_selected);
        sexMale.setBackgroundResource(R.drawable.set_sex_bg);
    }

    @OnClick(R.id.tv_sex_male) void onMaleSelected(){
        sex =0;
        sexMale.setBackgroundResource(R.drawable.sex_selected);
        sexFemale.setBackgroundResource(R.drawable.set_sex_bg);
    }

    /**
     * 前往tag设置界面
     */
    @OnClick(R.id.btn_next) void nextClicked() {
        String nickName = edtNickName.getText()+"";
        String birthday = edtBirthday.getText()+"";
        String school = edtSchool.getText()+"";
        String city = edtCity.getText()+"";
        presenter.setUserInfo(nickName,headImage,birthday,sex,school,city);
    }

    @Override
    public void errorNickName() {
        edtNickName.setError("昵称格式不对");
    }

    @Override
    public void noSetNickName() {
        edtNickName.setError("昵称不能为空");
    }

    @Override
    public void errorBirthday() {
        edtBirthday.setError("生日日期不对");
    }

    @Override
    public void noSetSex() {
        showMessage("性别没有设置");
    }

    @Override
    public void goToNext() {
        Flow.get(getContext()).set(new Paths.RegisterFinishPath());
    }

    @Override
    public void showProgress() {
        EventBus.getDefault().post(new ProgressEvent(true));
    }

    @Override
    public void hideProgress() {
        EventBus.getDefault().post(new ProgressEvent(false));
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getContext(),message,Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onDetachedFromWindow() {
        EventBus.getDefault().unregister(this);
        super.onDetachedFromWindow();
    }
}
